﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ADECMS.UserPages
{
    public partial class Start : System.Web.UI.Page
    {
        ADECMSDBDataContext cmsdb;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                cmsdb = new ADECMSDBDataContext();
                NEW novost = cmsdb.NEWs.OrderByDescending(x => x.id).First();
                ClanakNaslov.Text = novost.title;
                ClanakText.Text = novost.text;
                ClanakAutorDat.Text = novost.USER.username + ", " + novost.news_date;
            }
            catch (Exception ex)
            {
                ClanakNaslov.Text = " Trenutno nema članaka ";
            }
            if (!Page.IsPostBack)
            {
                TextBox1.Visible = false;
                Submit.Visible = false;

                HttpCookie loginCookie = Request.Cookies["AuthCookie"];
                if (Request.IsAuthenticated)
                {
                    FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(loginCookie.Value);
                    Label1.Text = "Logovani ste " + ticket.Name.ToString();

                    var useri = from k in cmsdb.USERs
                                from p in cmsdb.USER_TYPEs
                                where k.username == ticket.Name && k.user_type_id == p.id && p.type == "Administrator"
                                select k;
                    try
                    {
                        USER kor = useri.First();
                        MenuItem apanel = new MenuItem("Admin panel");
                        Menu1.Items.Add(apanel);

                    }
                    catch (System.InvalidOperationException)
                    {
                        return;
                    }
                }
            }
        }
        

        protected void SignOut_Click(object sender, EventArgs e)
        {
            FormsAuthentication.SignOut();
            Response.Redirect("/Default.aspx");
        }

        protected void AdminPanel_Click(object sender, EventArgs e)
        {

        }

        protected void DodajNovost_Click(object sender, EventArgs e)
        {
            Response.Redirect("/UserPages/AddNews.aspx");
        }

        protected void ButtonPregledProfila_Click(object sender, EventArgs e)
        {

        }

        protected void MojProfil_Click(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }

        protected void Button2_Click(object sender, EventArgs e)
        {

        }

        protected void ButtonPretraga_Click(object sender, EventArgs e)
        {
            Response.Redirect("/UserPages/Search.aspx");
        }

        protected void UcitajTekstIKomentare(String nasl)
        {
            try
            {
                TextBox1.Visible = true;
                Submit.Visible = true;
                NEW nov = cmsdb.NEWs.Single(n => n.title == nasl);
                ClanakText.Text = nov.text;
                ClanakAutorDat.Text = nov.USER.username + ", " + nov.news_date;
                ClanakAutorDat.Text += "<br>" + nov.CATEGORY.name;
                int cnt = nov.COMMENTs.Count;
                if (cnt == 0)
                    KomentarText.Text = "<br><hr>Ovaj članak nema komentara";
                else
                {
                    String kom = "<br><hr>";
                    for (int i = 0; i < cnt; i++)
                    {

                        kom += nov.COMMENTs.ElementAt(i).text + "<br>"
                            + nov.COMMENTs.ElementAt(i).USER.username + ", "
                            + nov.COMMENTs.ElementAt(i).comment_date + "<br><br>";
                    }

                    KomentarText.Text = kom;
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void Submit_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text == "") return;
            COMMENT kom = new COMMENT();
            kom.comment_date = DateTime.Now;
            kom.text = TextBox1.Text;
            //pokupi username korisnika
            HttpCookie loginCookie = Request.Cookies["AuthCookie"];
            FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(loginCookie.Value);
            USER korisnik = cmsdb.USERs.Single(k => k.username == ticket.Name.ToString());
            NEW nov = cmsdb.NEWs.Single(n => n.title == ClanakNaslov.Text);
            korisnik.COMMENTs.Add(kom);
            nov.COMMENTs.Add(kom);

            cmsdb.COMMENTs.InsertOnSubmit(kom);
            cmsdb.SubmitChanges();
            TextBox1.Text = "";
            UcitajTekstIKomentare(ClanakNaslov.Text);
        }

        protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "BtnClick")
            {
                ClanakNaslov.Text = e.CommandArgument.ToString();
                //Response.Write(e.CommandArgument);
                NEW nov = cmsdb.NEWs.Single(n => n.title == ClanakNaslov.Text);
                ClanakText.Text = nov.text;
                ClanakAutorDat.Text = nov.USER.name + ", " + nov.news_date;
                UcitajTekstIKomentare(ClanakNaslov.Text);

            }
        }

        protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
        {
            switch (e.Item.Text)
            {
                case "Sign out":
                    FormsAuthentication.SignOut();
                    Response.Redirect("/Default.aspx");
                    break;
                case "Dodaj novost":
                    Response.Redirect("/UserPages/AddNews.aspx");
                    break;
                case "Pretraga":
                    Response.Redirect("/UserPages/Search.aspx");
                    break;
                case "Admin panel":
                    Response.Redirect("/UserPages/AdminPanel.aspx");
                    break;
                case "File upload":
                    Response.Redirect("/UserPages/FileUpload.aspx");
                    break;
                case "File download":
                    Response.Redirect("/UserPages/FileDownload.aspx");
                    break;
                case "Upload prezentacije":
                    Response.Redirect("/UserPages/PresentationUpload.aspx");
                    break;
                case "Pregled prezentacije":
                    Response.Redirect("/UserPages/PresentationView.aspx");
                    break;
                case "Pregledaj profil":
                    Response.Redirect("/UserPages/ProfileView.aspx");
                    break;
                case "Edituj profil":
                    Response.Redirect("/UserPages/ProfileEdit.aspx");
                    break;
            }
        }
    }
}
